//
//  SSIMClient.m
//  SSIMClient
//
//  Created by 秦雨 on 17/5/18.
//  Copyright © 2017年 秦雨. All rights reserved.
//

#import "SSIMClient.h"
#import "NetCenter.h"
#import "NIMMessageCenter.h"
#import "NIMGlobalProcessor.h"
#import "SSIMAppTypeDefines.h"
#import "nimncconstdef.h"
#import "NIMMessageCenter.h"
#import "SSIMMessageErrorManager.h"
@interface SSIMClient()<BMKGeneralDelegate>

@property(nonatomic,strong)NSMutableDictionary *blockDict;
@property(nonatomic,strong)BMKMapManager *mapManager;
@end

@implementation SSIMClient

SingletonImplementation(SSIMClient)

-(instancetype)init
{
    self = [super init];
    if (self) {
        //初始化全局处理器
        [[NIMGlobalProcessor sharedInstance] InitProcessor];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recvMessage:) name:NC_SDK_MESSAGE object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recvNetworkChange:) name:NC_SDK_NETWORK object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(recvPushToVC:) name:NC_SDK_PUSHTOVC object:nil];
        [self registerBaiduMap];
        self.blockDict = [[NSMutableDictionary alloc] initWithCapacity:10];
        NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
        [user setBool:YES forKey:@"msg_status"];
        [user synchronize];
    }
    return self;
}

-(void)setupCoreDataStack
{
    [MagicalRecord setupCoreDataStackWithAutoMigratingSqliteStoreNamed:@"qianbaoIM.mom"];
}

-(void)setLoginInfo:(SSIMLogin *)loginInfo
{
    [self setValue:@(loginInfo.user_id) forKey:NSStringFromSelector(@selector(owerid))];
    [self setValue:@(loginInfo.ap_id) forKey:NSStringFromSelector(@selector(appid))];
//    [[NIMSysManager sharedInstance] setOwerid:loginInfo.user_id];
    [[NIMSysManager sharedInstance] SetLoginInfo:loginInfo];
}

-(void)setIsQbaoLoginSuccess:(BOOL)isQbaoLoginSuccess
{
    [[NIMSysManager sharedInstance] setIsQbaoLoginSuccess:isQbaoLoginSuccess];
}

-(void)Connect:(NSString *)host port:(unsigned int)port
{
    [[NetCenter sharedInstance] Connect:host port:port domain:true];
}

-(void)reConnect
{
    [[NetCenter sharedInstance] CacheConnect];
}

-(void)disConnect
{
    [[NetCenter sharedInstance] DisConnect];
}

-(void)checkConnect
{
    [[NetCenter sharedInstance] CheckConnect];

}

-(void)sendMessage:(SSIMMessage *)message callBackBlock:(SSIMCallBack)callBackBlock
{
    //判断传入信息是否正确
    SDK_MESSAGE_RESULT result = [[SSIMMessageErrorManager sharedInstance] getMessageStatus:message];
    
    if (result != SDK_MESSAGE_OK) {
        callBackBlock(nil,@(result));
        return;
    }
    int64_t msgid = [NIMBaseUtil GetServerTime];
    [self.blockDict setObject:callBackBlock forKey:@(msgid)];
    [[NIMMessageCenter sharedInstance] sendSDKMessage:message messageid:msgid];
}


-(void)recvMessage:(NSNotification *)notification
{
    id object = notification.object;
    if ([object isKindOfClass:[SSIMMessage class]]) {
        SSIMMessage *message = object;
        SSIMCallBack callBackBlock = [self.blockDict objectForKey:@(message.messageId)];
        callBackBlock(message,@(SDK_MESSAGE_OK));
        [self.blockDict removeObjectForKey:@(message.messageId)];
    }else if ([object isKindOfClass:[NSString class]]){
        int64_t msgid = [object longLongValue];
        SSIMCallBack callBackBlock = [self.blockDict objectForKey:@(msgid)];
        callBackBlock(nil,@(SDK_MESSAGE_TIMEOUT));
        [self.blockDict removeObjectForKey:@(msgid)];
    }
}

-(void)recvNetworkChange:(NSNotification *)notification
{
    SDK_NET_STATUS status = (SDK_NET_STATUS)[notification.object intValue];
    switch (status) {
        case SDK_CONNECTED:
            if ([_delegate respondsToSelector:@selector(onConnected)]) {
                [_delegate onConnected];
            }
            break;
        case SDK_CLOSED:
            if ([_delegate respondsToSelector:@selector(onClose)]) {
                [_delegate onClose];
            }
            break;
        case SDK_FAILURE:
            if ([_delegate respondsToSelector:@selector(onConnectFailure)]) {
                [_delegate onConnectFailure];
            }
            break;
        case SDK_ERROR:
            if ([_delegate respondsToSelector:@selector(onError)]) {
                [_delegate onError];
            }
            break;
        case SDK_BEKICKED:
            if ([_delegate respondsToSelector:@selector(onBekicked)]) {
                [_delegate onBekicked];
            }
            break;
        case SDK_LOGINED:
            if ([_delegate respondsToSelector:@selector(onLogined)]) {
                [_delegate onLogined];
            }
            break;
            
        default:
            break;
    }
}

-(void)recvPushToVC:(NSNotification *)notification
{
    SDK_PUSH_VC_TYPE status = (SDK_PUSH_VC_TYPE)[notification.object intValue];
    switch (status) {
        case SDK_PUSH_Task:
            if ([_delegate respondsToSelector:@selector(nimPushToTaskVC)]) {
                [_delegate nimPushToTaskVC];
            }
            break;
        case SDK_PUSH_Subscribe:
            if ([_delegate respondsToSelector:@selector(nimPushToSubscribeVC)]) {
                [_delegate nimPushToSubscribeVC];
            }
            break;
        case SDK_PUSH_OfficialList:
            if ([_delegate respondsToSelector:@selector(nimPushToOfficialListVC)]) {
                [_delegate nimPushToOfficialListVC];
            }
            break;
        case SDK_PUSH_OfficialChat:
            if ([_delegate respondsToSelector:@selector(nimPushToOfficialChatVC)]) {
                [_delegate nimPushToOfficialChatVC];
            }
            break;
        case SDK_PUSH_BackHome:
            if ([_delegate respondsToSelector:@selector(nimBackToHomeVC)]) {
                [_delegate nimBackToHomeVC];
            }
            break;
        default:
            break;
    }
}

#pragma mark -- 加载百度地图
-(void)registerBaiduMap
{
    _mapManager = [[BMKMapManager alloc]init];
    
    BOOL ret = [_mapManager start:kMapAppkey generalDelegate:self];
    if (!ret) {
        NSLog(@"manager start failed!");
    }
}

@end
